using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Net.Sockets;

namespace DidaManager.Native.Network
{
    public class NetworkAdapter
    {
        public string? Name { get; set; }
        public string? Description { get; set; }
        public string? Id { get; set; }
        public string? MacAddress { get; set; }
        public string? AdapterType { get; set; }
        public long Speed { get; set; }
        public string? Status { get; set; }
        public bool IsReceiveOnly { get; set; }
        public bool SupportsMulticast { get; set; }
        public List<string>? IpAddresses { get; set; }
        public List<string>? GatewayAddresses { get; set; }

        public List<NetworkAdapter> GetNetworkAdapters()
        {
            var adapters = new List<NetworkAdapter>();
            foreach (var ni in NetworkInterface.GetAllNetworkInterfaces())
            {
                var adapter = new NetworkAdapter
                {
                    Name = ni.Name,
                    Description = ni.Description,
                    Id = ni.Id,
                    MacAddress = ni.GetPhysicalAddress().ToString(),
                    AdapterType = ni.NetworkInterfaceType.ToString(),
                    Speed = ni.Speed,
                    Status = ni.OperationalStatus.ToString(),
                    IsReceiveOnly = ni.IsReceiveOnly,
                    SupportsMulticast = ni.SupportsMulticast,
                    IpAddresses = new List<string>(),
                    GatewayAddresses = new List<string>()
                };

                var ipProps = ni.GetIPProperties();
                foreach (var gw in ipProps.GatewayAddresses)
                {
                    adapter.GatewayAddresses.Add(gw.Address.ToString());
                }

                foreach (var ip in ipProps.UnicastAddresses)
                {
                    if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
                    {
                        adapter.IpAddresses.Add(ip.Address.ToString());
                    }
                }
                // 没有IpAddresses 不添加到adapters
                if (adapter.IpAddresses.Count > 0)
                {
                    adapters.Add(adapter);
                }
            }
            return adapters;
        }
    }
} 